package com.shrcl.engineerprocess.domain.impl;

import com.google.common.base.Objects;
import com.shrcl.engineerprocess.domain.ActorsDAO;
import com.shrcl.engineerprocess.domain.EngineerProcessTaskExtension;
import com.shrcl.engineerprocess.domain.impl.EngineerProcessTaskExtensionImpl;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.langlab.domain.organization.Party;
import edu.fudan.langlab.domain.task.TSKTask;
import edu.fudan.langlab.domain.task.TSKTaskDAO;
import edu.fudan.langlab.domain.task.TSKTaskOwner;
import edu.fudan.langlab.domain.task.Task;
import edu.fudan.langlab.domain.task.TaskEventListener;
import edu.fudan.langlab.domain.workflow.BusinessProcess;
import edu.fudan.langlab.domain.workflow.ProcessTask;
import edu.fudan.langlab.domain.workflow.Token;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrcl.engineerprocess.domain.ProcessTask2TskTaskAdaptor")
public class ProcessTask2TskTaskAdaptorImpl extends BaseService implements TaskEventListener {
  @Autowired
  private TSKTaskDAO taskDAO;
  
  @Autowired
  private ActorsDAO actorsDAO;
  
  public void afterTaskCreated(final Task task) {
    if ((task instanceof ProcessTask)) {
      InputOutput.<String>println("task instanceof ProcessTask");EngineerProcessTaskExtension engineerProcessTaskExtension=EngineerProcessTaskExtensionImpl.create(getObjectFactory());
      final Procedure1<EngineerProcessTaskExtension> _function = new Procedure1<EngineerProcessTaskExtension>() {
          public void apply(final EngineerProcessTaskExtension it) {
            it.bindTo(((ProcessTask) task));
            String _entityName = task.getEntityName();
            it.setTaskDescription(_entityName);
            Token _token = ((ProcessTask) task).getToken();
            BusinessProcess _bp = _token.getBp();
            String _processName = _bp.getProcessName();
            it.setTaskName(_processName);
            Party _owner = task.getOwner();
            TSKTaskOwner _asTSKTaskOwner = ProcessTask2TskTaskAdaptorImpl.this.actorsDAO.asTSKTaskOwner(_owner);
            it.addOwner(_asTSKTaskOwner);
            TSKTaskOwner _owner_1 = it.getOwner();
            boolean _notEquals = (!Objects.equal(_owner_1, null));
            Validates.Assert(_notEquals, "\u4EFB\u52A1\u6240\u6709\u4EBA\u4E3A\u7A7A\uFF01");
          }
        };
      final EngineerProcessTaskExtension ext = ObjectExtensions.<EngineerProcessTaskExtension>operator_doubleArrow(engineerProcessTaskExtension, _function);
      final Procedure1<TSKTask> _function_1 = new Procedure1<TSKTask>() {
          public void apply(final TSKTask it) {
            Date _now = DateUtil.now();
            it.setGenDate(_now);
            it.start();
            it.startProcess();
          }
        };
      this.taskDAO.createTask(_function_1, ext);
    }
  }
  
  public void beforeTaskDelete(final Task task) {
    if ((task instanceof ProcessTask)) {
      final ProcessTask tsk = ((ProcessTask) task);
      IGenericQuery<EngineerProcessTaskExtension> query=getObjectFactory().createGenericQuery(EngineerProcessTaskExtension.class,"select e from com.shrcl.engineerprocess.domain.impl.EngineerProcessTaskExtensionImpl as e   where ((e.processTask) = (:tsk)) and  e.active = 1      ").setParameter("tsk",tsk);
      EngineerProcessTaskExtension _uniqueResult = query.uniqueResult();
      final Procedure1<EngineerProcessTaskExtension> _function = new Procedure1<EngineerProcessTaskExtension>() {
          public void apply(final EngineerProcessTaskExtension it) {
            it.unbind();
            TSKTask _task = it.getTask();
            _task.finish();
          }
        };
      ObjectExtensions.<EngineerProcessTaskExtension>operator_doubleArrow(_uniqueResult, _function);
    }
  }
}
